/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy=new ListNode(-1);
        
        dummy->next=head;

        ListNode* left=dummy;
        ListNode* right=dummy;

        while(n--)
        {
            right=right->next;
        }
        while(right->next)
        {
            left=left->next;
            right=right->next;
        }
        ListNode* tmp=left->next;
        left->next=tmp->next;
        delete tmp;
        return dummy->next;

    }
};